import storage from "good-storage";

const SEARCH_KEY = "__search__";
const SEARCH_MAX = 15;

const HISTORY_KEY = "__history__";
const HISTORY_MAX_LENGTH = 50;

const PLAYLIST_KEY = "__playList__";
// ${storage.get("userInfo", {}).userId}

//合并对象数组并去重
function processingData(arr1, arr2, location) {
  let json = location === "end" ? arr1.concat(arr2) : arr2.concat(arr1); //两个数组对象合并
  let newJson = []; //盛放去重后数据的新数组
  //循环json数组对象的内容
  for (let item1 of json) {
    let flag = true; //建立标记，判断数据是否重复，true为不重复
    //循环新数组的内容
    for (let item2 of newJson) {
      //让json数组对象的内容与新数组的内容作比较，相同的话，改变标记为false
      // if (item1.trackId && item1.trackId == item2.trackId) {
      //   item1 = item2;
      //   flag = false;
      // }
      if (item1.trackId && item1.trackId == item2.trackId) {
        if (location === "end") {
          newJson.splice(newJson.indexOf(item2), 1);
        } else {
          item1 = item2;
          flag = false;
        }
      }
    }
    //判断是否重复
    if (flag) {
      newJson.push(item1);
    }
  }
  // 播放列表大于2000首 保留2000首(尾部删除)
  if (newJson.length > 2000) {
    newJson.length = 2000;
  }
  return newJson;
}
// 批量删除
function clearArr(arr1, arr2) {
  arr2.forEach((item) => {
    arr1 = arr1.filter((a) => a.trackId != item.trackId);
  });
  return arr1;
}
// function clearArr(arr1,arr2) {
//   for(let i = arr1.length -1; i >= 0; i--){
//     for(let j = arr2.length -1; j >= 0; j--) {
//       if(arr1[i].trackId == arr2[j].trackId) {
//         arr1.splice(i, 1);
//       }
//     }
//   }
//   return arr1;
// }
// 插入
function insertArray(arr, val, compare, maxLen) {
  const index = arr.findIndex(compare);
  if (index === 0) {
    return;
  }
  if (index > 0) {
    arr.splice(index, 1);
  }
  arr.unshift(val);
  if (maxLen && arr.length > maxLen) {
    arr.pop();
  }
}

// 删除
function deleteFromArray(arr, compare) {
  const index = arr.findIndex(compare);
  if (index > -1) {
    arr.splice(index, 1);
  }
}

// 存储历史搜索数据
export function saveSearch(query) {
  let searches = storage.get(SEARCH_KEY, []);
  insertArray(
    searches,
    query,
    (item) => {
      return item === query;
    },
    SEARCH_MAX
  );
  storage.set(SEARCH_KEY, searches);
  return searches;
}

// 读取历史搜索数据
export function getSearch() {
  return storage.get(SEARCH_KEY, []);
}

// 删除单个搜索历史
export function deleteSearch(query) {
  let searches = storage.get(SEARCH_KEY, []);
  deleteFromArray(searches, (item) => {
    return item === query;
  });
  storage.set(SEARCH_KEY, searches);
  return searches;
}

// 清空搜索历史
export function clearSearch() {
  storage.remove(SEARCH_KEY);
  return [];
}

// 保存播放列表
export function addPlayList(list, location) {
  let playList = storage.get(PLAYLIST_KEY, []);
  let newPlayList = processingData(playList, list, location);
  storage.set(PLAYLIST_KEY, newPlayList);
  return newPlayList;
}

//移除播放列表
export function delPlayList(list) {
  let playList = storage.get(PLAYLIST_KEY, []);
  let newPlayList = clearArr(playList, list);
  storage.set(PLAYLIST_KEY, newPlayList);
  return newPlayList;
}

// 添加最近播放列表
export function saveHistory(song) {
  let songs = storage.get(HISTORY_KEY, []);
  insertArray(
    songs,
    song,
    (item) => {
      return song.trackId === item.trackId;
    },
    HISTORY_MAX_LENGTH
  );
  storage.set(HISTORY_KEY, songs);
  return songs;
}

// 移除最近播放列表
export function deleteHistory(song) {
  let songs = storage.get(HISTORY_KEY, []);
  deleteFromArray(songs, (item) => {
    return song.id === item.id;
  });
  storage.set(HISTORY_KEY, songs);
  return songs;
}

// 清空最近播放列表
export function clearHistory() {
  storage.remove(HISTORY_KEY);
  return [];
}
